<?php
/**
 * 支付密码.
 * User: Liu
 * Date: 2017/3/10
 * Time: 8:49
 */
namespace app\mobile\controller;

use think\Cache;
use think\Request;

use app\mobile\model\Member;
use think\Session;

class PayPass extends BaseController
{
    /**
     * 修改密码
     */
    public function edit()
    {
        //用户绑定的手机号码
        $info = Member::where('id', Session::get('member_id'))->field('username,pay_pass')->find();

        return $this->fetch('', [
            'info' => $info,
        ]);
    }

    /**
     * 验证手机号的正确性
     */
    public function bind_phone()
    {
        // 参数
        $mobile = Request::instance()->param('tel');

        $condition = [
            'id' => Session::get('member_id'),
            'username' => $mobile,
        ];

        $member = (new Member())->where($condition)->find();

        if ($member) {
            return ['code' => 200];
        }
        return ['code' => 0, 'message' => '输入的手机号与绑定的号码不符'];

    }

    /**
     * 发送验证码 [支付密码验证]
     */
    public function getVerif(Request $request)
    {
        if ($request->isPost()) {
            $tel = $request->param('tel');
            $code = mt_rand(100000, 999999);  //验证码随机数
            $content = "您的验证码是" . $code;
            $result = sendSMS($tel, $content);
            //发送验证码
            if ($result) {
                Cache::set('pay_pass_yzm', $code, 300);
                return ['code' => 200, 'message' => $code];
            }
            return ['code' => 400, 'message' => '网络繁忙'];

        }
    }


    /**
     * 绑定手机数据提交处理
     */
    public function bind_act(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $code = Cache::get('pay_pass_yzm');
            if (!$code) {
                return ['code' => 401, 'message' => '验证码已失效,请重新获取'];
            }
            if ($code == $data['code']) {
                return ['code' => 200];
            }
            return ['code' => 400, 'message' => '验证码不正确'];
        }
    }


    /**
     * 验证旧支付密码
     */
    public function check(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $model = new Member();
            //查找用户原有密码
            $pay = $model->where('id',Session::get('member_id'))->value('pay_pass');
            if ($request->has('old_pwd','post')){
                if ($pay && $pay != md5(md5($data['old_pwd']))){
                    return ['code'=>401,'message'=>'旧密码不正确'];
                }
            }
            if (md5(md5($data['new_pwd'])) == $pay) {return ['code'=>400,'message'=>'新密码不能与旧密码一致'] ;}
            if ($data['new_pwd'] != $data['confirm_new_pwd']){return ['code'=>400,'message'=>'两次密码不一致'] ;}
            //设置或更改密码
            $res = $model
                ->where('id',Session::get('member_id'))
                ->update(['pay_pass'=>md5(md5($data['new_pwd']))]);
            return $res||$res === 0 ?['code'=>200,'message'=>'操作成功']:['code'=>404,'message'=>'网络繁忙,请重试'];
        }
        return $this->fetch('check');
    }


    /**
     * 输入新的支付密码
     */
    public function modify()
    {
        if (Request::instance()->isPost()) {
            $pay_pass = (new Member())->where(['id' => Session::get('member_id')])->value('pay_pass');

            return $pay_pass;
        }
        return $this->fetch('modify');
    }


    /**
     * 设置新的密码
     */
    public function set_up()
    {
        if (Request::instance()->isPost()) {
            $pay_pass = Request::instance()->post('upwd');
            $operation = (new Member())->where(['id' => Session::get('member_id')])->update(['pay_pass' => $pay_pass]);

            if ($operation) {
                return true;
            }
            return false;
        }
    }
}